/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package estates.util;

import com.sun.webui.jsf.component.DropDown;
import com.sun.webui.jsf.component.TextField;
import java.util.HashMap;

/**
 *
 * @author Karol
 */
public class ParamGenerator {
    public static Integer ConvertSelection(DropDown d){
        return new Integer(Integer.parseInt(d.getSelected().toString()));
    }

    public static String ConvertToString(TextField t){
        return t.getValue().toString();
    }

    public static Integer ConvertToInteger(TextField t){
        return new Integer(Integer.parseInt(t.getValue().toString()));
    }
    
    public static Boolean ConvertToBoolean(DropDown d){
        String tmp = d.getSelected().toString();
        return new Boolean(!tmp.equals("-1") && tmp.equals("0"));
    }

    public static void parseBoolFields(HashMap destination, String[] fields, DropDown[] components, boolean allowNull){
        for(int i=0; i<fields.length; i++){
            if(allowNull && (components[i].getSelected() == null || components[i].getSelected().toString().equals("-1"))){
                continue;
            }else
            if(!components[i].getSelected().toString().equals("-1")){
                destination.put(fields[i], ConvertToBoolean(components[i]));
            }
        }
    }

    public static void parseStrFields(HashMap destination, String[] fields, TextField[] components, boolean allowNull){
        for(int i=0; i<fields.length; i++){
            if(allowNull && components[i].getValue() == null){
                continue;
            }else
            if(components[i].getValue() != null && components[i].getValue().toString().length()>0){
                destination.put(fields[i], ConvertToString(components[i]));
            }
        }
    }

    public static void parseIntFields(HashMap destination, String[] fields, TextField[] components, boolean allowNull){
        for(int i=0; i<fields.length; i++){
            if(allowNull && components[i].getValue() == null){
                continue;
            }else
            if(components[i].getValue() != null && components[i].getValue().toString().length()>0){
                destination.put(fields[i], ConvertToInteger(components[i]));
            }
        }
    }

    public static void parseSelection(HashMap destination, String[] fields, DropDown[] components, boolean allowNull){
        for(int i=0; i<fields.length; i++){
            if(allowNull && components[i].getSelected() == null){
                continue;
            }else
            if(!components[i].getSelected().toString().equals("-1")){
                destination.put(fields[i], ConvertSelection(components[i]));
            }
        }
    }
}
